package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryAsphaltPenetrometerBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryAsphaltPenetrometerVo;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltPenetrometer;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltPenetrometerMapper;
import com.ruoyi.zhgd.service.INLaboratoryAsphaltPenetrometerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青针入度仪Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryAsphaltPenetrometerServiceImpl implements INLaboratoryAsphaltPenetrometerService {

    private final NLaboratoryAsphaltPenetrometerMapper baseMapper;

    /**
     * 查询沥青针入度仪
     */
    @Override
    public NLaboratoryAsphaltPenetrometerVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青针入度仪列表
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltPenetrometerVo> queryPageList(NLaboratoryAsphaltPenetrometerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryAsphaltPenetrometer> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryAsphaltPenetrometerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青针入度仪列表
     */
    @Override
    public List<NLaboratoryAsphaltPenetrometerVo> queryList(NLaboratoryAsphaltPenetrometerBo bo) {
        LambdaQueryWrapper<NLaboratoryAsphaltPenetrometer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryAsphaltPenetrometer> buildQueryWrapper(NLaboratoryAsphaltPenetrometerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryAsphaltPenetrometer> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryAsphaltPenetrometer::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), NLaboratoryAsphaltPenetrometer::getFlag, bo.getFlag());
        lqw.eq(bo.getRealTimeDisplacement() != null, NLaboratoryAsphaltPenetrometer::getRealTimeDisplacement, bo.getRealTimeDisplacement());
        lqw.eq(bo.getRealTimeTemperature() != null, NLaboratoryAsphaltPenetrometer::getRealTimeTemperature, bo.getRealTimeTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginalData()), NLaboratoryAsphaltPenetrometer::getOriginalData, bo.getOriginalData());
        lqw.eq(StringUtils.isNotBlank(bo.getResId()), NLaboratoryAsphaltPenetrometer::getResId, bo.getResId());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleId()), NLaboratoryAsphaltPenetrometer::getSampleId, bo.getSampleId());
        return lqw;
    }

    /**
     * 新增沥青针入度仪
     */
    @Override
    public Boolean insertByBo(NLaboratoryAsphaltPenetrometerBo bo) {
        NLaboratoryAsphaltPenetrometer add = BeanUtil.toBean(bo, NLaboratoryAsphaltPenetrometer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青针入度仪
     */
    @Override
    public Boolean updateByBo(NLaboratoryAsphaltPenetrometerBo bo) {
        NLaboratoryAsphaltPenetrometer update = BeanUtil.toBean(bo, NLaboratoryAsphaltPenetrometer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryAsphaltPenetrometer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青针入度仪
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils queryPage(Page page, String resId) {
        List<Map> list = baseMapper.queryPages(page, resId);
        return new PageUtils(page.setRecords(list));
    }
}
